export default {
	
	filters: {
		numToString: function(v) {
			return v.toString()
		}
	},
	
	//钩子生命周期函数会执行两次，mixins一次，子组件一次
	created() {
		
		if(!this.openMultiPage) {
			this.getList()
		}
	},
	
	data() {
		return {
			//分页信息
			page: {
				current: 1,
				sizes: [10,50,100,200],
				size: 10,
				total: 0
			},
			
			pageArr: {
				def: {
					current: 1,
					sizes: [10,50,100,200],
					size: 10,
					total: 0
				}
			},
			
			//多选checkbox
			multipleSelection: [],
			
			//preUrl
			preUrl: '',
			
			//是否显示主内容区的loading
			loading: true,
			
			//开启多个分页bar
			openMultiPage: false,
			
			pageKey: 'def',
		}
	},
	
	computed: {
		//选中记录的id组成的数组
		ids() {
			return this.multipleSelection.map(v => v.id)
		}
	},
	
	methods: {
		//pageArr初始化
		initPageArr() {
			
		},
		//
		showLoading() {
			if (this.loading){
				this.layout.showLoading()
			}
		},
		hideLoading() {
			if(this.loading) {
				this.layout.hideLoading()
			}
		},
		//获取分页列表的url
		getListUrl() {
			console.log('父组件getListUrl')
			return `/admin/${this.preUrl}/${this.page.current}?limit=${this.page.size}`
		},
		
		//处理列表结果
		getListResult(data) {
			
		},
		
		getListResultByParams(params) {
			let url = this.getListUrl()
			if(params instanceof Object) {
				for(let key in params) {
					url += '&' + key + '=' + params[key]
				}
			}
			console.log(url)
			return url
		},
		
		getListPlus(params) {
			//下面这一行是为了父组件在执行时this.layout不报错
			if(this.preUrl == '') return
			this.showLoading()
			let url = this.getListResultByParams(params)
			this.axios.get(url,{token: true})
				.then(res => {
					let data = res.data.data
					this.getListResult(data)
					this.page.total = data.totalCount
					this.hideLoading()
				})
				.catch(err => {
					this.hideLoading()
				})
		},
		
		//获取列表
		getList() {
			//下面这一行是为了父组件在执行时this.layout不报错
			if(this.preUrl == '') {
				console.log('父组件getList return')
				return
			}
			this.showLoading()
			let url = this.getListUrl()
			console.log('请求url:', url)
			this.axios.get(url,{token: true})
				.then(res => {
					let data = res.data.data
					this.getListResult(data)
					if (!this.openMultiPage) {
						this.page.total = data.totalCount
					} else {
						this.pageArr[this.preUrl].total = data.totalCount
					}
					this.hideLoading()
				})
				.catch(err => {
					this.hideLoading()
				})
		},
		
		//选中table中的checkbox
		handleSelectionChange(val) {
			this.multipleSelection = val;
		},
		
		//分页方法，每页的数目
		handleSizeChange(val) {
			console.log('当前页码：', val)
			if (this.openMultiPage) {
				if(this.pageArr[this.preUrl]) {
					this.pageArr[this.preUrl].size = val
				}
				this.getList()
			} else {
				this.page.size = val
				this.getList()
			}
			
		},
		//分页方法，当前选中页
		handleCurrentChange(val) {
			console.log('点击页码，' + val)
			if (this.openMultiPage) {
				if(this.pageArr[this.preUrl]) {
					this.pageArr[this.preUrl].current = val
					console.log('修改后', this.pageArr[this.preUrl].current)
				} 
				this.getList()
			} else {
				this.page.current = val
				this.getList()
			}
			
		},
		
		addOrEdit(data, id = 0) {
			//修改
			this.showLoading()
			let msg = id > 0 ? '修改' : '新增'
			let url = id > 0 ? '/admin/'+ this.preUrl +'/' + id : 'admin/' + this.preUrl
			this.axios.post(url, data, {token: true})
				.then(res => {
					this.getList()
					this.hideLoading()
					this.$message({
						message: msg + '成功',
						type: 'success'
					})
				}).catch(err => {
					this.hideLoading()
				})
		},
		
		addOrEditPromise(data, id = 0) {
			return new Promise((resolve, reject) => {
				this.showLoading()
				let msg = id > 0 ? '修改' : '新增'
				let url = id > 0 ? '/admin/'+ this.preUrl +'/' + id : 'admin/' + this.preUrl
				this.axios.post(url, data, {token: true})
					.then(res => {
						this.getList()
						this.hideLoading()
						this.$message({
							message: msg + '成功',
							type: 'success'
						})
						resolve('success')
					}).catch(err => {
						this.hideLoading()
						reject(err)
					})
			})
		},
		
		deleteAllUrl() {
			return '/admin/'+ this.preUrl +'/delete_all'
		},
		
		//批量删除
		deleteAll() {
			if(this.ids.length === 0) {
				return this.$message({
					message: '你还没有选中项',
					type: 'warning'
				});
			}
			this.$confirm('此操作将删除选中项, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				let url = this.deleteAllUrl()
				this.showLoading()
				this.axios.post(url, {ids: this.ids}, {token: true})
				.then(res => {
					this.hideLoading()
					this.getList()
					this.multipleSelection = []
					this.$message({
						type: 'success',
						message: '删除成功!'
					});
				}).catch(err => {
					this.hideLoading()
				})
			}).catch(() => {
				this.$message({
				type: 'info',
				message: '已取消删除'
				});
			});
		},
		
		//修改状态
		changeStatus(item, isSwitch = false) {
			//todo 请求服务端
			let status = 0
			if (!isSwitch) {
				status = item.status === 1 ? 0 : 1
			} else {
				status = item.status
			}
			let msg = status === 1 ? '启用' : '禁用'
			this.showLoading()
			this.axios.post('/admin/'+ this.preUrl +'/' + item.id + '/update_status', {
				status: status
			}, {token: true}).then(res => {
				item.status = status
				this.hideLoading()
				this.$message({
					message: msg + '成功',
					type: 'success'
				})
			}).catch(err => {
				this.hideLoading()
			})
			
		},
		
		//删除单个
		deleteItem(item) {
			this.$confirm('此操作将删除该条数据, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.showLoading()
				this.axios.post('/admin/'+ this.preUrl +'/' + item.id + '/delete',{},{token:true})
					.then(res => {
						this.getList()
						this.hideLoading()
						this.$message({
							type: 'success',
							message: '删除成功!'
						});
					}).catch(err => {
						this.hideLoading()
					})
				
			}).catch(() => {
				this.$message({
				type: 'info',
				message: '已取消删除'
				});
			});
		},
	}
}